<template>
	<view>
		<!-- 若需显示自定义导航栏，需要在App.vue中引入Vue -->
		<cu-custom bgColor="bg-gradual-green" :isBack="true">
			<block slot="content">我的订单</block>
		</cu-custom>

		<scroll-view scroll-y="true">
			<view class="orderListView">

				<view class="emptyList" v-if="orderList.length == 0">
					没有该类订单嗷~
				</view>


				<view class="orderListItem" v-for="order in orderList">
					<view class="orderInfoView">
						<view class="orderImageView">
							<image :src="'https://www.2306.tech/CCShop/'+order.orderImage" style="width: 200upx; height: 200upx; border-radius: 10upx;"></image>
						</view>
						<view class="orderDetailInfoView">
							<view class="orderName">{{order.orderName}} </view>
							<view class="orderCount">单价：<text class="text-price">{{order.orderSinglePrice}}</text> <text style="margin-left: 15upx;">数量：{{order.orderBuyCount}}</text>
							</view>
							<view class="orderTotalPrice">{{order.orderStatus}}总金额：<text class="text-price text-red" style="font-size: 18px;">{{order.orderTotalFee}}</text></view>
						</view>
					</view>
					<view class="orderButtonArea">
						<view class="createTime">{{order.createTime.split(" ")[0]}}</view>
						<view class="buttons">
							<button class="cu-btn round shadow btn" :data-id="order.orderId" v-if="pageType == 0" @click="doCancel(order.orderId)">取消订单</button>
							<button class="cu-btn round bg-blue shadow btn" :data-id="order.orderId" v-if="pageType == 0" @click="goPay(order.orderId)">支付订单</button>
							<text class="text-green" v-if="pageType == 1" style="margin-right: 10upx;">拣货中...</text>
							<text class="text-green" style="margin-right: 10upx;" v-if="order.needEvaluation ==  0">订单已完成</text>
							
							<button class="cu-btn round bg-green shadow btn" :data-id="order.orderId" @click="doConfirm(order.orderId)" v-if="pageType == 6">确认收货</button>
							<button class="cu-btn round bg-pink shadow btn" :data-id="order.orderId" @click="doEvaluation(order.orderId)" v-if="order.needEvaluation == 1">评价订单</button>

						</view>
					</view>
				</view>
			</view>
		</scroll-view>

	</view>
</template>

<script>
	import {
		getOrderByStatus,
		updateOrderStatusToCancle,
		confirmOrder,
		
	} from '../../api/orderAPI.js'
	export default {
		data() {
			return {
				orderList: [
					// 	{
					// 	"orderImage": "images/goods/K901/K901-0.jpg",
					// 	"orderSinglePrice": 32.9,
					// 	"orderAddress": "北京城市学院",
					// 	"orderId": "G20201214202852989",
					// 	"orderTotalFee": 32.9,
					// 	"createTime": "2020-12-14 20-28-52",
					// 	"orderBuyCount": "1",
					// 	"orderName": "红心火龙果4个装 新鲜水果 红肉火龙果大果单果约300g-400g 水果"
					// },
				],
				pageType: 0
			}
		},
		methods: {
			goPay(orderId) {
				uni.navigateTo({
					url: '../pay/pay?type=1&id=' + orderId
				})
			},

			doCancel(orderId) {
				uni.showModal({
					title: '提示',
					content: '确认取消订单吗?',
					confirmColor: 'red',
					confirmText: '取消订单',
					cancelText: '再想想',
					success: (resp) => {
						if (resp.confirm) {
							updateOrderStatusToCancle(orderId).then(resp => {
								if ('success' == resp.result) {
									uni.showToast({
										title: '订单已取消'
									})
									uni.startPullDownRefresh({

									})
									getOrderByStatus(this.pageType).then(resp => {
										this.orderList = resp
									})
									uni.stopPullDownRefresh()

								}
							})
						}
					}
				})
			},

			doConfirm(orderId) {
				uni.showModal({
					title: '提示',
					content: '确认已收到物品了吗？',
					confirmColor: 'red',
					confirmText: '确认收货',
					cancelText: '取消',
					success: (res) => {
						if (res.confirm) {
							confirmOrder(orderId).then(resp => {
								if (true == resp.rs1) {
									uni.showToast({
										title: '订单已完成'
									})
									uni.startPullDownRefresh({

									})
									getOrderByStatus(this.pageType).then(resp => {
										this.orderList = resp
									})
									uni.stopPullDownRefresh()

								}
							})
						}
					}
				})
			},
			
			doEvaluation(orderId) {
				uni.navigateTo({
					url:'../evaluation/evaluation?orderId='+orderId
				})
			}
		},

		onLoad(params) {
			uni.startPullDownRefresh({

			})
			let status = params.status;
			if (!status) {
				status = 0
			}
			this.pageType = status
			if(status != 7) {
				getOrderByStatus(status).then(resp => {
					this.orderList = resp
				})
			} else {
				let arr = [];
				getOrderByStatus(7).then(resp => {
					resp.forEach(item => {
						item.needEvaluation = 1
						arr.push(item)
					})
				})
				getOrderByStatus(8).then(resp => {
					resp.forEach(item => {
						item.needEvaluation = 0
						arr.push(item)
					})
				})
				this.orderList = arr
				
			}
			
			
			
			
		},
		onPullDownRefresh() {
			setTimeout(() => {
				// getOrderByStatus(this.pageType).then(resp => {
				// 	this.orderList = resp
				// })
				
				// if(this.pageType == 7) {
				// 	let arr = [];
				// 	getOrderByStatus(7).then(resp => {
				// 		resp.forEach(item => {
				// 			arr.push(item);
				// 		})
				// 	})
				// 	getOrderByStatus(8).then(resp => {
				// 		resp.forEach(item => {
				// 			arr.push(item)
				// 		})
				// 	})
				// 	this.orderList = arr
					
				// }
				
				if(this.pageType != 7) {
					getOrderByStatus(status).then(resp => {
						this.orderList = resp
					})
				} else {
					let arr = [];
					getOrderByStatus(7).then(resp => {
						resp.forEach(item => {
							arr.push(item);
						})
					})
					getOrderByStatus(8).then(resp => {
						resp.forEach(item => {
							arr.push(item)
						})
					})
					this.orderList = arr
					
				}
				
				uni.stopPullDownRefresh()
			}, 2000)

		}
	}
</script>

<style>
	@import url("./orderList.css");
</style>
